/*****左侧导航与右侧tabs功能----------begin******/
/**
* 左侧导航菜单点击事件
*/
$(function(){
	
	   
	
	$.extend($.fn.validatebox.defaults.rules, {
	    /*必须和某个字段相等*/
	    equalTo: { validator: function (value, param) { return $(param[0]).val() == value; }, message: '字段不匹配' }
	   });

	$.extend($.fn.validatebox.defaults.rules, {     
	    idcared: {     
	        validator: function(value,param){    
	            var flag= isCardID(value);  
	            return flag==true?true:false;    
	        },     
	        message: '不是有效的身份证号码'    
	    }     
	});  
	
	//获取cookie颜色主题
	if($.cookie("easyuiThemeName")!=null && $.cookie("easyuiThemeName")!=""){
		changeTheme($.cookie("easyuiThemeName"));
	}
}); 
var idaCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
function isCardID(sId){   
    var iSum=0 ;  
    var info="" ;  
    if(!/^\d{17}(\d|x)$/i.test(sId)) return "你输入的身份证长度或格式错误";   
    sId=sId.replace(/x$/i,"a");   
    if(idaCity[parseInt(sId.substr(0,2))]==null) return "你的身份证地区非法";   
    sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));   
    var d=new Date(sBirthday.replace(/-/g,"/")) ;  
    if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))return "身份证上的出生日期非法";   
    for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11) ;  
    if(iSum%11!=1) return "你输入的身份证号非法";   
    return true;//aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女")   
}   

function addMenuTabsPanel(){//点击添加右侧tabs新页面
	$('.sider li a').click(function(){
	var classId = 'index';
	$(this).parent().parent().click();//使上级变色(选中颜色)
	var subtitle = $(this).text();
	var url = $(this).attr('cmshref');
	var rel = $(this).attr('rel');
	//左侧直接打开弹窗sss
	if(rel=='dialog'){
		var type = $(this).attr('type');
		//alert(type);
		openDialog(type,url,subtitle);
		return false;
	}
	//更新内容到右侧的tabs内容区
	if(!$('#tabs_'+classId).tabs('exists',subtitle)){//存在
		$('#tabs_'+classId).tabs('add',{
			title:subtitle,
			content:subtitle,
			closable:true,
			href:url,
			tools:[{
					iconCls:'icon-mini-refresh',
					handler:function(){
						$('#tabs_'+classId).tabs('addEventParam');
						setTimeout(function(){//之所以要延迟是因为上方停止传播(冒泡算法)需要要时间
							updateTab(classId,url,subtitle);
						}, 300);
					}
				}]
		});
		return false;
	}else{
		$('#tabs_'+classId).tabs('select',subtitle);
		updateTab(classId,url,subtitle);
		return false;
	}
	});
}
function showDirector(subtitle,url,rel,closable)
{
	var classId = 'index';
	if(closable==false){
		if(!$('#tabs_'+classId).tabs('exists',subtitle)){
			$('#tabs_'+classId).tabs('add',{
				title:subtitle,
				content:subtitle,
				closable:false,
				closed:false,
				cache:true,
				href:url,
				tools:[{
					iconCls:'icon-mini-refresh',
					handler:function(){
						$('#tabs_'+classId).tabs('addEventParam');
						setTimeout(function(){
							updateTab(classId,url,subtitle);
						}, 300);
					}
				}]
			});

			return false;
		}else{
			$('#tabs_'+classId).tabs('select',subtitle);
			updateTab(classId,url,subtitle);

			return false;
		}
	}else{
		if(!$('#tabs_'+classId).tabs('exists',subtitle)){
			$('#tabs_'+classId).tabs('add',{
				title:subtitle,
				content:subtitle,
				closable:true,
				closed:true,
				cache:true,
				href:url,
				tools:[{
					iconCls:'icon-mini-refresh',
					handler:function(){
						$('#tabs_'+classId).tabs('addEventParam');
						setTimeout(function(){
							updateTab(classId,url,subtitle);
						}, 300);
					}
				}]
			});

			return false;
		}else{
			$('#tabs_'+classId).tabs('select',subtitle);
			updateTab(classId,url,subtitle);

			return false;
		}
	}


}
/**
* 更新tab功能
*/
function updateTab(classId,url,subtitle){
var tab = $('#tabs_'+classId).tabs('getSelected');
$('#tabs_'+classId).tabs('update', {  	
    tab: tab,  	
    options: {  		
        title: subtitle, 
        href: url,
		tools:[{
			iconCls:'icon-mini-refresh',
			handler:function(){
				$('#tabs_'+classId).tabs('addEventParam');
				setTimeout(function(){
					updateTab(classId,url,subtitle);
				}, 300);
			}
		}]
    }  
});  
//tab.panel('refresh', url);
}
/*****左侧导航与右侧tabs功能----------end******/

/*****顶部特效----------begin******/
/**
* 更换EasyUI主题的方法
* @param themeName
* 主题名称
*/
changeTheme = function(themeName) {
var $easyuiTheme = $('#easyuiTheme');
var url = $easyuiTheme.attr('href');
var href = url.substring(0, url.indexOf('themes')) + 'themes/' + themeName + '/easyui.css';
$easyuiTheme.attr('href', href);
var $iframe = $('iframe');
if ($iframe.length > 0) {
for ( var i = 0; i < $iframe.length; i++) {
var ifr = $iframe[i];
$(ifr).contents().find('#easyuiTheme').attr('href', href);
}
}
$.cookie('easyuiThemeName', themeName, {
expires : 7
});
};
function logoutFun(b) {
	$.getJSON('/userController/logout.action', function(result) {
		if (b) {
			location.replace('/index.jsp');
		} else {
			$('#sessionInfoDiv').html('');
			$('#user_login_loginDialog').dialog('open');
			$('#layout_east_onlineDatagrid').datagrid('load', {});
		}
	});
}
function userInfoFun() {
	$('<div/>').dialog({
		href : '/userController/userInfo.action',
		width : 490,
		height : 285,
		modal : true,
		title : '用户信息',
		buttons : [ {
			text : '修改密码',
			iconCls : 'icon-edit',
			handler : function() {
				var d = $(this).closest('.window-body');
				$('#user_userInfo_form').form('submit', {
					url : '/userController/modifyCurrentUserPwd.action',
					success : function(result) {
						try {
							var r = $.parseJSON(result);
							if (r.success) {
								d.dialog('destroy');
							}
							$.messager.show({
								title : '提示',
								msg : r.msg
							});
						} catch (e) {
							$.messager.alert('提示', result);
						}
					}
				});
			}
		} ],
		onClose : function() {
			$(this).dialog('destroy');
		},
		onLoad : function() {
		}
	});
}
/*****顶部特效--------end******/

/*****通用函数--------begin******/
/*
 *  submitForm  提交表单时执行
 *  classId 为当前表单的id
 */
function submitForm(classId){
    var url = $('#form_'+classId).attr('action');
    alert("表单提交成功！，请修改jeasyui.common.js的submitForm方法！");
	$('#dialog_cms').dialog('close');
    return false;
    $('#form_'+classId).form('submit',{
        url:url,
        onSubmit:function(){
        //$('#dialog').dialog('refresh', '__APP__/Setting/add');
        //alert(url);
        //$('#dialog').dialog('close');
        },
        success:function(msg){
            var data = $.parseJSON(msg);
            //alert(data.msg+'=======dede====');
            //return false;
            formAjax(data,classId);
        }
    });

}
function formAjax(data,classId){
    //alert(classId);
    //return false;

    if(data.status==1){
        $.messager.alert(data.info,data.info,'error');
    }else if(data.status==2){
        $.messager.show({
            title:data.info,
            msg:data.info,
            timeout:5000,
            showType:'slide'
        });
        $('#treegrid_'+classId).treegrid('reload');
        if(data.isclose=='ok'){
            $('#dialog_'+classId).dialog('close');
            dialogOnClose(classId);
        }
    }
}
/*
 *openDialog 弹出框
 *href 传递控制器的url地址
 *title 弹出窗口的标题
 */
function openDialog(classId,href,title){
    $('#dialog_cms').dialog({
        href:href,
        width:1000,
        height:680,
        resizable:true,
        title:title,
        modal:true,
        resizable:true,
        collapsible:true,
        maximizable:true,
        cache: false,
        onClose:function(){
            dialogOnClose(classId);
        },
        buttons:[{
            text:'保存',
            iconCls:'icon-ok',
            handler:function(){
                submitForm(classId);
            }
        },{
            text:'取消',
            iconCls:'icon-cancel',
            handler:function(){
                dialogOnClose(classId);
            }
        }
        ]
    });
//$('#dialog'+classId).dialog('refresh', href);
}
/*
* 关闭dialog时，销毁dialog代码
*/
function dialogOnClose(classId){
    $('#dialog_cms').dialog('destroy');
    $('body.easyui-layout').append('<div id="dialog_cms"  data-options="iconCls:\'icon-save\'"></div>');
    var frame = $('iframe[src="about:blank"]');//destroy与iframe冲突问题，大概是内存释放的原因
    frame.remove();
}
/*****通用函数--------end******/

/*****news函数--------begin******/
var admin_news_viewFun = function(id){
		alert("打开前台页面");
};

var admin_news_cancelFun = function(id){
		var hrefcancel = 'news/del.html';
		var href = hrefcancel;
		var title = '删除信息';
		$.messager.confirm(title,href, function(){
			$.ajax({
				url:href,
				type:'post',
				data:{
					id:ids
				},
				dataType:'json',
				success:function(data){
					formAjax(data,classId);
				}
			});
		});//$
};
function admin_news_appendFun(classId,href,title) {
		$('#datagrid_newstab0').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
		$('<div></div>').dialog({
		href:href,
		width:1000,
		height:680,
		modal : true,
		resizable:true,
		collapsible:true,
		maximizable:true,
		title:title,
		buttons : [ {
			text : '增加',
			iconCls : 'icon-add',
			handler : function() {
				var d = $(this).closest('.window-body');	//上层弹窗
				$('#newsform').form('submit', {
				url : 'news/save.html',
				success : function(result) {
				try {
					var r = $.parseJSON(result);
					if (r.success) {
					$('#datagrid_newstab0').datagrid('insertRow', {
						index : 0,
						row : r.obj
					});
					d.dialog('destroy');
					}
					$.messager.show({
						title : '提示',
						msg : r.msg
					});
				}catch (e) {
				$.messager.alert('提示',result);
				}
				}
				});
			}
		} ],
		onClose : function() {
		$(this).dialog('destroy');
		}
		});
}
function admin_news_editFun(id) {
		href = 'news/edit.html?id='+id;
		title = '编辑文档';
		$('#datagrid_newstab0').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');	//清除选择
		$('<div></div>').dialog({
		href:href,
		width:1000,
		height:680,
		modal : true,
		resizable:true,
		collapsible:true,
		maximizable:true,
		title:title,
		buttons : [ {
			text : '编辑',
			iconCls : 'icon-edit',
			handler : function() {
				var d = $(this).closest('.window-body');	//上层弹窗
				$('#newseditform').form('submit', {
					url : 'news/editsave.html',
					success: function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									$('#datagrid_newstab0').datagrid('updateRow', {
										index: $('#datagrid_newstab0').datagrid('getRowIndex', id),
										row: r.obj
									});
									d.dialog('destroy');
								}
							} catch(e) {
								$.messager.alert('提示', result);
							}
					}
				});
			}
		} ],
        onClose: function() {
            $(this).dialog('destroy');
        },
        onLoad: function() {
            var index = $('#datagrid_newstab0').datagrid('getRowIndex', id);
            var rows = $('#datagrid_newstab0').datagrid('getRows');
            var o = rows[index];
            o.roleIds = stringToList(rows[index].roleIds);
            $('#newseditform').form('load', o);
        }
		});
}
function admin_news_removeFun() {
    var rows = $('#datagrid_newstab0').datagrid('getChecked');
    var ids = [];
    if (rows.length > 0) {
        $.messager.confirm('确认', '您是否要删除当前选中的项目？',
        function(r) {
            if (r) {
                var flag = true;
                $.ajax({
                    url: 'news/del.html',
                    data: {
                        ids: ids.join(',')
                    },
                    dataType: 'json',
                    success: function(result) {
                        if (result.success) {
                            $('#datagrid_newstab0').datagrid('load');
                            $('#datagrid_newstab0').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                        }
                        $.messager.show({
                            title: '提示',
                            msg: result.msg
                        });
                    }
                });
            }
        });
    } else {
        $.messager.show({
            title: '提示',
            msg: '请勾选要删除的记录！'
        });
    }
}
function admin_newscat_appendFun(classId,href,title) {
		$('<div></div>').dialog({
		href:href,
		width:600,
		height:400,
		modal : true,
		resizable:true,
		collapsible:true,
		maximizable:true,
		title:title,
		buttons : [ {
			text : '增加',
			iconCls : 'icon-add',
			handler : function() {
				var d = $(this).closest('.window-body');	//上层弹窗
				$('#newsaddform').form('submit', {
				url : 'news/newscatsave.html',
				success : function(result) {
				try {
					var r = $.parseJSON(result);
					if (r.success) {
						/*$('#treegrid_newssort').treegrid('append', {	//此段数据对显示效果有影响
							parent: r.obj.pid,
							data: [r.obj]
						});*/
						d.dialog('destroy');
						$('#treegrid_newssort').treegrid('reload');		//使用动态载入方式比较友好
					}
					$.messager.show({
						title : '提示',
						msg : r.msg
					});
				}catch (e) {
				$.messager.alert('提示',result);
				}
				}
				});
			}
		} ],
		onClose : function() {
		$(this).dialog('destroy');
		}
		});	
}
function admin_newscat_editFun(id) {
    if (id != undefined) {
        $('#treegrid_newssort').treegrid('select', id);
    }
    var node = $('#treegrid_newssort').treegrid('getSelected');
    $('<div></div>').dialog({
        href: 'news/newssortedit.html',
        width: 600,
        height: 400,
        modal: true,
        title: '编辑分类',
        buttons: [{
            text: '编辑',
            iconCls: 'icon-edit',
            handler: function() {
                var d = $(this).closest('.window-body');
                $('#newscateditform').form('submit', {
                    url: 'news/newscatsave.html',
                    success: function(result) {
                        try {
                            var r = $.parseJSON(result);
                            if (r.success) {
                                $('#treegrid_newssort').treegrid('reload');
                                d.dialog('destroy');
                            }
                        } catch(e) {
                            $.messager.alert('提示', result);
                        }
                    }
                });
            }
        }],
        onClose: function() {
            $(this).dialog('destroy');
        },
        onLoad: function() {
            //$('#newscateditform').form('load', node);	//自动载入节点信息
        }
    });
}

function admin_newscat_cancelFun(id) {
    if (id != undefined) {
        $('#treegrid_newssort').treegrid('select', id);
    }
    var node = $('#treegrid_newssort').treegrid('getSelected');
    if (node) {
        $.messager.confirm('询问', '您确定要删除【' + node.name + '】？',
        function(b) {
            if (b) {
                $.ajax({
                    url: 'news/catdel.html',
                    data: {
                        id: node.id
                    },
                    cache: false,
                    dataType: 'JSON',
                    success: function(r) {
                        if (r.success) {
                            $('#treegrid_newssort').treegrid('remove', r.obj); 	//显示删除效果
                            $('#treegrid_newssort').treegrid('reload');
                        }
                        $.messager.show({
                            msg: r.msg,
                            title: '提示'
                        });
                    }
                });
            }
        });
    }
}
/*****news函数--------end******/

/*****pictures函数--------begin******/
var admin_pics_viewFun = function(id){
		alert("打开前台页面");
};
var admin_pics_editFun = function(id){
		var classId = 'picstab0';
		var hrefadd = 'pictures/add.html';
		var title = '添加图片';
		openDialog(classId,hrefadd,title);
};
var admin_pics_cancelFun = function(id){
		var hrefcancel = 'pictures/del.html';
		var href = hrefcancel;
		var title = '删除信息';
		$.messager.confirm(title,href, function(){
			$.ajax({
				url:href,
				type:'post',
				data:{
					id:ids
				},
				dataType:'json',
				success:function(data){
					formAjax(data,classId);
				}
			});
		});//$
};
/*****pictures函数--------end******/

/*****friendlink函数--------begin******/
var admin_links_viewFun = function(id){
		alert("打开前台页面");
};
var admin_links_editFun = function(id){
		var classId = 'picstab0';
		var hrefadd = 'pictures/add.html';
		var title = '添加链接';
		openDialog(classId,hrefadd,title);
};
var admin_links_cancelFun = function(id){
		var hrefcancel = 'pictures/del.html';
		var href = hrefcancel;
		var title = '删除链接';
		$.messager.confirm(title,href, function(){
			$.ajax({
				url:href,
				type:'post',
				data:{
					id:ids
				},
				dataType:'json',
				success:function(data){
					formAjax(data,classId);
				}
			});
		});//$
};
/*****friendlink函数--------end******/


/************防止冒泡算法导致tabs刷新时候调用两次(停止事件传播)----start************************/
$.extend($.fn.tabs.methods, {   
    /**
     * tabs组件每个tab panel对应的小工具条tools绑定的事件没有传递事件参数
     * 本函数修正这个问题  
     * @param {[type]} jq [description]  
     */  
    addEventParam: function(jq) {   
        return jq.each(function() {   
            var that = this;   
            var headers = $(this).find('>div.tabs-header>div.tabs-wrap>ul.tabs>li');   
            headers.each(function(i) {   
                var tools = $(that).tabs('getTab', i).panel('options').tools;
                if (typeof tools != "string") {  
                    $(this).find('>span.tabs-p-tool a').each(function(j) { 
                        $(this).unbind('click').bind("click", {   
                            handler: tools[j].handler   
                        }, function(e) {   
                            if ($(this).parents("li").hasClass("tabs-disabled")) {   
                                return;   
                            }   
                            e.data.handler.call(this, e);
                        });   
                    });   
                }   
            });
        });   
    }   
});  
/************防止冒泡算法导致tabs刷新时候调用两次----end************************/







/*
 * MAP对象，实现MAP功能
 *
 * 接口：
 * size()     获取MAP元素个数
 * isEmpty()    判断MAP是否为空
 * clear()     删除MAP所有元素
 * put(key, value)   向MAP中增加元素（key, value) 
 * remove(key)    删除指定KEY的元素，成功返回True，失败返回False
 * get(key)    获取指定KEY的元素值VALUE，失败返回NULL
 * element(index)   获取指定索引的元素（使用element.key，element.value获取KEY和VALUE），失败返回NULL
 * containsKey(key)  判断MAP中是否含有指定KEY的元素
 * containsValue(value) 判断MAP中是否含有指定VALUE的元素
 * values()    获取MAP中所有VALUE的数组（ARRAY）
 * keys()     获取MAP中所有KEY的数组（ARRAY）
 *
 * 例子：
 * var map = new Map();
 *
 * map.put("key", "value");
 * var val = map.get("key")
 * ……
 *
 */
function Map() {
	this.elements = new Array();

	//获取MAP元素个数
	this.size = function() {
		return this.elements.length;
	}

	//判断MAP是否为空
	this.isEmpty = function() {
		return (this.elements.length < 1);
	}

	//删除MAP所有元素
	this.clear = function() {
		this.elements = new Array();
	}

	//向MAP中增加元素（key, value) 
	this.put = function(_key, _value) {
		this.elements.push( {
			key : _key,
			value : _value
		});
	}

	//删除指定KEY的元素，成功返回True，失败返回False
	this.remove = function(_key) {
		var bln = false;
		try {
			for (i = 0; i < this.elements.length; i++) {
				if (this.elements[i].key == _key) {
					this.elements.splice(i, 1);
					return true;
				}
			}
		} catch (e) {
			bln = false;
		}
		return bln;
	}

	//获取指定KEY的元素值VALUE，失败返回NULL
	this.get = function(_key) {
		try {
			for (i = 0; i < this.elements.length; i++) {
				if (this.elements[i].key == _key) {
					return this.elements[i].value;
				}
			}
		} catch (e) {
			return null;
		}
	}

	//获取指定索引的元素（使用element.key，element.value获取KEY和VALUE），失败返回NULL
	this.element = function(_index) {
		if (_index < 0 || _index >= this.elements.length) {
			return null;
		}
		return this.elements[_index];
	}

	//判断MAP中是否含有指定KEY的元素
	this.containsKey = function(_key) {
		var bln = false;
		try {
			for (i = 0; i < this.elements.length; i++) {
				if (this.elements[i].key == _key) {
					bln = true;
				}
			}
		} catch (e) {
			bln = false;
		}
		return bln;
	}

	//判断MAP中是否含有指定VALUE的元素
	this.containsValue = function(_value) {
		var bln = false;
		try {
			for (i = 0; i < this.elements.length; i++) {
				if (this.elements[i].value == _value) {
					bln = i;
				}
			}
		} catch (e) {
			bln = false;
		}
		return bln;
	}
	
	//根据值返回key
	this.getKeyByValue = function(_value) {
		try {
			for (i = 0; i < this.elements.length; i++) {
				if (this.elements[i].value == _value) {
					return this.elements[i].key;
				}
			}
		} catch (e) {
			return null;
		}
	}

	//获取MAP中所有VALUE的数组（ARRAY）
	this.values = function() {
		var arr = new Array();
		for (i = 0; i < this.elements.length; i++) {
			arr.push(this.elements[i].value);
		}
		return arr;
	}

	//获取MAP中所有KEY的数组（ARRAY）
	this.keys = function() {
		var arr = new Array();
		for (i = 0; i < this.elements.length; i++) {
			arr.push(this.elements[i].key);
		}
		return arr;
	}
}


